package com.fuqi.android.zhongcai.business.shuig.view.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.result.bean.VisitBean;
import com.fuqi.android.zhongcai.business.shuig.lintener.ConfirmListener;
import com.fuqi.android.zhongcai.business.shuig.request.result.bean.Check;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChouChaAdapter extends BaseAdapter {
	private List<Check> listBean = new ArrayList<>();
	private Context context;
	private Map<String,View> mgvs;
	ConfirmListener listener;
	public ChouChaAdapter(Context context, List<Check> listBean) {
		this.context = context;
		this.listBean = listBean;
		mgvs = new HashMap<String,View>();
	}
	public void setDate(List<Check> listBean) {
		this.listBean = listBean;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listBean.size();
	}

	@Override
	public Check getItem(int position) {
		return listBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_choucha, null);
			viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.gvItem = (GridView) convertView.findViewById(R.id.gv_item);
			viewHolder.etRemark = (EditText) convertView.findViewById(R.id.et_remark);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.gvItem.setTag(getItem(position));
		viewHolder.gvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Check v = (Check) ((View)view.getParent()).getTag();
				mgvs.put(v.getZD_ID(),(View)view.getParent());
				if(v.equals(listBean.get(listBean.size() - 1))){
					GridView lastGv = (GridView)view.getParent();
					if(lastGv.equals(view.getParent())){
						if(listener != null) {
							VisitBean answer = ((AnswerAdapter) lastGv.getAdapter()).getDate().get(lastGv.getCheckedItemPosition());
							listener.onListener(answer.getBaseCode().equals("1"));
						}
					}
				}
			}
		});
		final List<VisitBean> datass = makeAnswers(getItem(position));
		AnswerAdapter mTypeAdapter = new AnswerAdapter(context,datass);

		viewHolder.tvTitle.setText(getItem(position).getNAME());
		viewHolder.gvItem.setAdapter(mTypeAdapter);
		return convertView;
	}

	List<VisitBean> makeAnswers(Check check){
		List<VisitBean> datas = new ArrayList<>();
		VisitBean bean1 = new VisitBean();;
		VisitBean bean2 = new VisitBean();
		bean1.setName("是");
		bean2.setName("否");
		bean1.setBaseCode("1");
		bean2.setBaseCode("0");

		datas.add(bean1);
		datas.add(bean2);
		return datas;
	}

	public void setListener(ConfirmListener listener){
		this.listener = listener;
	}

	private class ViewHolder{
        TextView tvTitle;
        GridView gvItem;
		EditText etRemark;
    }
	public JsonArray getItem(){
		JsonArray jsonarray = new JsonArray();

		for (int j = 0; j < listBean.size(); j++){
			JsonObject json = new JsonObject();
			Check question = listBean.get(j);
			GridView gv = (GridView)mgvs.get(question.getZD_ID());
			if(gv != null) {
				SparseBooleanArray poss = gv.getCheckedItemPositions();
				for (int i = 0; i < poss.size(); i++) {
					int key = poss.keyAt(i);
					if (poss.get(key)) {
						VisitBean answer = ((AnswerAdapter) gv.getAdapter()).getDate().get(key);
						json.addProperty("QUESTION_CODE", question.getP_BM());
						json.addProperty("RESULT", answer.getBaseCode());
						json.addProperty("ORDY_BY", question.getORDY_BY());
						json.addProperty("REMARK", "");

						jsonarray.add(json);
					}
				}
			}
		}
		return jsonarray;
	}


}
